package insight.google.game.era.data;


import insight.google.game.era.core.IGameCenter;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;





/**
 * Catches calls to the ApiProxy and can perform 
 * measurements.
 */
public class TransactionHandler {
  
	private PersistenceManager pm = null;
	private Transaction tx;
	
	public TransactionHandler(){
		pm = DataStoreManager.getInstance().getPersistentManager();
		tx = pm.currentTransaction();
		tx.begin();
	}
	
	@SuppressWarnings("finally")
	public boolean save(Object obj)throws Exception{
		try {
			pm.makePersistent(obj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.rollBack();
			
		}finally{
			this.rollBack();
			throw new Exception();
		}
//		return true;
	}
	
	public boolean commit(){
		if(tx != null){
			try{
				tx.commit();
			}catch(Exception ex){
				
			}finally{
				this.rollBack();
			}
		}
		return true;
	}
	
	private void rollBack(){
		if(tx != null && tx.isActive()){
			tx.rollback();
		}
	}
	
	public void close(){
		this.rollBack();
		pm.close();
	}
	
	public PersistenceManager getPersistenceManager(){
		return this.pm;
	}
 
}